﻿using System;
using System.Diagnostics;
using System.IO;

namespace Administrator
{
    public class DebugLogger
    {
        private string m_LogFileName;
        private FileStream m_LogFileStream;
        private TextWriterTraceListener m_LogFileWriter;
        private TextWriterTraceListener m_ConsoleLogWriter;


//[Conditional(conditionString: "DEBUG")] 
        public DebugLogger()
        {
            m_LogFileName = "CeAdmin.log";

            m_LogFileStream = new FileStream(m_LogFileName,
                           FileMode.Create, FileAccess.Write);

            m_LogFileWriter = new TextWriterTraceListener(m_LogFileStream);

            m_ConsoleLogWriter =
                           new TextWriterTraceListener(System.Console.Out);

            Debug.Listeners.Add(m_LogFileWriter);
            Debug.Listeners.Add(m_ConsoleLogWriter);
        }

       
        public DebugLogger(string logFileName, bool bUseConsole)
        {
            
            if (logFileName != string.Empty)
            {
                m_LogFileName = "CeAdmin.log";
                m_LogFileStream = new FileStream(m_LogFileName,
                               FileMode.Create, FileAccess.Write);

                m_LogFileWriter = new TextWriterTraceListener(m_LogFileStream);
               

                Debug.Listeners.Add(m_LogFileWriter);    
            }

            if (bUseConsole)
            {
                m_ConsoleLogWriter =
                              new TextWriterTraceListener(System.Console.Out);
                Debug.Listeners.Add(m_ConsoleLogWriter);
            }
                
        }

        [Conditional(conditionString: "DEBUG")] 
        public void PrintException(Exception e)
        {
            Debug.WriteLine("Свойства исключения:");
            Debug.WriteLine("TargetSite = {0}", e.TargetSite);
            Debug.WriteLine("Source = {0}", e.Source);
            Debug.WriteLine("Message = {0}", e.Message);
            if (e.InnerException == null)
                Debug.WriteLine("InnerException = null");
            else Debug.WriteLine("InnerException = {0}",
                    e.InnerException.Message);
            Debug.WriteLine("StackTrace = {0}", e.StackTrace);
            Debug.WriteLine("GetBaseException = {0}",
                    e.GetBaseException());
            Debug.WriteLine("-------------------------------------");
        }

         [Conditional(conditionString: "DEBUG")] 
        public void WriteLine(string line)
        {
            Debug.WriteLine(line);
        }


    }
}
